<template>
  <div>
  	<div id="bcid"style="background-color: #000000;width: 100%;height: 100%;">
			<div style="height:40%;"></div>
			<p class="tip">...载入中...</p>
		</div>
		<footer>
			<div class="fbt" @click="back()">取　 消</div>
			<div class="fbt" @click="scanPicture()">从相册选择二维码</div>
		</footer>
  </div>
</template>

<script>
	import { Toast } from 'mint-ui'
	var ws=null,wo=null;
	var domready=false;
export default {
	  data () {
	    return {
	    }
	  },
	  mounted: function() {
	  	if(window.plus){
	  		domready=true;
				this.plusReady();
			}else{
				document.addEventListener('plusready', plusReady, false);
			}
			// 监听DOMContentLoaded事件
		/*	document.addEventListener('DOMContentLoaded', function(){
				console.log(666);
				domready=true;
				this.plusReady();
			}, false);*/
	  },
	  methods:{
	  	onmarked(type, result, file){
	  		switch(type){
	    	case plus.barcode.QR:
	    	type = 'QR';
	    	break;
	    	case plus.barcode.EAN13:
	    	type = 'EAN13';
	    	break;
	    	case plus.barcode.EAN8:
	    	type = 'EAN8';
	    	break;
	    	default:
	    	type = '其它'+type;
	    	break;
		    }
		    scan.close();
		    if(result){
		    	result = result.replace(/\"/g,"");
		    }
				this.$router.replace({
					path: '/customerDetails/'+result
				})
	  },
	  scanPicture(){
		let _self = this;
		if(!plus){
			Toast("无法获取相册!");
			return false;
		}
	  	plus.gallery.pick(function(path){
		    plus.barcode.scan(path,_self.onmarked,function(error){
					plus.nativeUI.alert('无法识别此图片');
				});
	    }, function(err){
	        console.log('Failed: '+err.message);
	    });
	  },
	  plusReady:function(){
	  	if(ws||!window.plus||!domready){
				return;
			}
	  	if('denied' == plus.navigator.checkPermission('CAMERA')){
        Toast("未获取摄像头权限，请设置");
        return;
			}
	  	scan = new plus.barcode.Barcode('bcid');
	  	scan.onmarked=this.onmarked;
	  	scan.start();
	  	console.log("plusready执行了");
		/*	// 获取窗口对象
			ws=plus.webview.currentWebview();
			wo=ws.opener();
			// 开始扫描
			ws.addEventListener('show', function(){
				scan2=new plus.barcode.Barcode('bcid');
			    scan2.onmarked=this.onmarked;
			    scan2.start({conserve:true,filename:'_doc/barcode/'});
			}, false);
			// 显示页面并关闭等待框
		    ws.show('pop-in');
		    wo.evalJS('closeWaiting()');*/
	  },
	  back:function(){
		if(scan){
			scan.close();
		}
		this.$router.replace('/assistantIndex');
	  }
	}
}
</script>
<style scoped="scoped">
#bcid {
	width: 100%;
	position: absolute;
	top: 0px;
	bottom: 44px;
	text-align: center;
}
.tip {
	color: #FFFFFF;
	font-weight: bold;
	text-shadow: 0px -1px #103E5C;
}
footer {
	width: 100%;
	height: 54px;
	position: absolute;
	bottom: 0px;
	line-height: 54px;
	text-align: center;
	color: #FFF;
}
.fbt {
	width: 50%;
	height: 100%;
	background-color:#ef7d2e;
	float: left;
}
.fbt:active {
  	-webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
	box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
}
</style>
